package com.example.dao;

import com.example.entity.Order;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class OrderDao {

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/elm",
                "root",
                "123456"
        );
    }

    public Integer saveOrder(Order order) {
        String sql = "INSERT INTO orders (userId, shopId, orderDate, deliveryPrice, orderTotal, deliverAddress, orderState) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            pstmt.setString(1, order.getUserId());
            pstmt.setInt(2, order.getShopId());
            pstmt.setString(3, order.getOrderDate());
            pstmt.setDouble(4, order.getDeliveryPrice());
            pstmt.setDouble(5, order.getOrderTotal());
            pstmt.setString(6, order.getDeliverAddress());
            pstmt.setInt(7, order.getOrderState());
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Order getOrderById(Integer orderId) {
        String sql = "SELECT * FROM orders WHERE orderId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, orderId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("orderId"));
                order.setUserId(rs.getString("userId"));
                order.setShopId(rs.getInt("shopId"));
                order.setOrderDate(rs.getString("orderDate"));
                order.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                order.setOrderTotal(rs.getDouble("orderTotal"));
                order.setDeliverAddress(rs.getString("deliverAddress"));
                order.setOrderState(rs.getInt("orderState"));
                return order;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Order> listOrderByUserId(String userId) {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM orders WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, userId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("orderId"));
                order.setUserId(rs.getString("userId"));
                order.setShopId(rs.getInt("shopId"));
                order.setOrderDate(rs.getString("orderDate"));
                order.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                order.setOrderTotal(rs.getDouble("orderTotal"));
                order.setDeliverAddress(rs.getString("deliverAddress"));
                order.setOrderState(rs.getInt("orderState"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    public Integer updateOrder(Order order) {
        String sql = "UPDATE orders SET orderState = ? WHERE orderId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, order.getOrderState());
            pstmt.setInt(2, order.getOrderId());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}